/**
 * 地址列表
 */
class addressList extends eui.Component{
    private return:eui.Rect;
    private addAddress:eui.Button;
    private list:eui.List;
    private _arrayData:eui.ArrayCollection;
    private _addressData:Array<any> = [];

    private _exGoods:boolean = false;
    private _addAddressUI:addAddress;

    private _model:model;
    public constructor(exGoods:boolean = false){
        super();
        this._exGoods = exGoods;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "addressListSkin";
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this);
        this.addAddress.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(!this._addAddressUI){
                this._addAddressUI = new addAddress();
            }else{
                this._addAddressUI.reset();
            }
            this.addChild(this._addAddressUI);
        },this);

        Dispatcher.ins.addEventListener("addressList",this.assignValue,this);
        Dispatcher.ins.addEventListener("editAddress",this.getList,this);
        Dispatcher.ins.addEventListener("deleteAddress",this.getList,this);

        this._arrayData =  new eui.ArrayCollection(this._addressData);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = addressIR;

        this.list.addEventListener("details",this.resetDetails,this);
        this.list.addEventListener("edit",this.toEdit,this);
        this.list.addEventListener("delete",this.toDelete,this);
        this.list.addEventListener("setDefault",this.setDefault,this);

        this.getList();
    }
    public getList():void{
        let dataPost:any = {"node":1};
        socket.request("addressList",dataPost,1);
    }
    private resetDetails(evt:GameEvent):void{
        let data = evt.data;
        this.dispatchEventWith("changeDetail",true,data);
    }
    private toEdit(evt:GameEvent):void{
        let data = evt.data;
        if(!this._addAddressUI){
            this._addAddressUI = new addAddress("edit",data.UserName,data.PhoneNum,data.Province,data.City,data.Area,data.Address,data.Choice,data.Id);
        }else{
            this._addAddressUI.reset("edit",data.UserName,data.PhoneNum,data.Province,data.City,data.Area,data.Address,data.Choice,data.Id);
        }
        this.addChild(this._addAddressUI);
    }
    private toDelete(evt:GameEvent):void{
        var data = evt.data;
        if(this._exGoods && this._addressData.length == 1){
            let toast = new Toast();
            toast.show("如确认删除请至物流中心管理地址页删除");
            this.addChild(toast);
            return;
        }
        if(!this._model){
            this._model = new model("是否确认删除该地址?");
            this._model.Id = data.Id;
            this._model.addEventListener("delete",this.postDelete,this);
        }else{
            this._model.Id = data.Id;
        }
        this.addChild(this._model);
    }
    private postDelete(evt:GameEvent):void{
        let num = evt.data;
        let dataPost = {"id":num};
        socket.request("deleteAddress",dataPost);
        this.removeChild(this._model);
    }
    private setDefault(evt:GameEvent):void{
        let obj = evt.data;
        let dataPost:any;
        dataPost = {
            "userName":obj.UserName,
            "province":obj.Province,
            "city":obj.City,
            "area":obj.Area,
            "address":obj.Address,
            "phoneNum":obj.PhoneNum,
            "choice":obj.Choice == 0 ? 1 : 0,
            "id":obj.Id
        }
        socket.request("editAddress",dataPost);
    }

    public assignValue(evt: GameEvent):void{
        let obj = evt.data;
        if(obj.note !== 1) return;
        let list = obj.list;
        let arr = [];
        for(let i = 0,lg = list.length;i < lg;i ++){
            let trObj = list[i];
            trObj.detailed = trObj.Province + trObj.City + trObj.Area + trObj.Address;
            arr.push(trObj);
        }
        this._addressData = arr;
        this._arrayData = new eui.ArrayCollection(this._addressData);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
}
class addressIR extends eui.ItemRenderer{
    private edit:eui.Rect;
    private delete:eui.Rect;
    private default:eui.Image;
    private setDefault:eui.Rect;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "addressIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            this.parent.dispatchEventWith("details",true,this.data);
        },this);
        this.edit.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.parent.dispatchEventWith("edit",true,this.data);
        },this);
        this.delete.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.parent.dispatchEventWith("delete",true,this.data);
        },this);
        this.setDefault.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.parent.dispatchEventWith("setDefault",true,this.data);
        },this);
    }
    protected dataChanged():void{
        if(this.data.Choice == 0){
            this.default.visible = true;
        }else{
            this.default.visible = false;
        }
    }
}